package com.byt.framlib.commonwidget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;

import com.byt.framlib.R;

/**
 * des:加载页面内嵌提示
 * 直接调用
 */
public class LoadingTip extends LinearLayout {

    private Context context;
    private ImageView img_tip_logo;
    private ImageView progress;
    private TextView tv_tips;
    private LinearLayout ll_show_load_data;
    private ImageView img_show_load;
    private TextView bt_operate;
    private String errorMsg;
    private onReloadListener onReloadListener;
    private AnimationDrawable animationDrawable1;
    private LinearLayout ll_loading_tip;

    public LoadingTip(Context context) {
        super(context);
        initView(context);
    }

    public LoadingTip(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LoadingTip(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LoadingTip(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    //分为服务器失败，网络加载失败、数据为空、加载中、完成四种状态
    public static enum LoadStatus {
        sereverError, error, empty, loading, finish
    }

    private void initView(Context context) {
        this.context = context;
        View.inflate(context, R.layout.dialog_loading_tip, this);
        ll_loading_tip = findViewById(R.id.ll_loading_tip);
        img_tip_logo = findViewById(R.id.img_tip_logo);
        progress = findViewById(R.id.progress);
        tv_tips = findViewById(R.id.tv_tips);
        bt_operate = findViewById(R.id.bt_operate);
        ll_show_load_data = findViewById(R.id.ll_show_load_data);
        img_show_load = findViewById(R.id.img_show_load);
        //重新尝试
        ll_show_load_data.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View view) {
                if (onReloadListener != null) {
                    onReloadListener.reload(view);
                }
            }
        });
        progress.setImageResource(R.drawable.loading_dialog_anim);
        animationDrawable1 = (AnimationDrawable) progress.getDrawable();
        animationDrawable1.start();
        setVisibility(View.GONE);
    }

    public void setTips(String tips) {
        if (TextUtils.isEmpty(tips)) {
            tv_tips.setVisibility(GONE);
        } else {
            tv_tips.setVisibility(VISIBLE);
            if (tv_tips != null) {
                tv_tips.setText(tips);
            }
        }
    }

    public void setTipLogo(@DrawableRes int resId) {
        if (img_tip_logo != null) {
            img_tip_logo.setImageResource(resId);
        }
    }

    public void setTipLogo(@DrawableRes int resId, int width, int height) {
        if (img_tip_logo != null) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
                    height);//两个400分别为添加图片的大小
            img_tip_logo.setLayoutParams(params);
            img_tip_logo.setImageResource(resId);
        }
    }

    public void setLinearLayoutBgColor(@ColorInt int color) {
        if (ll_loading_tip != null) {
            ll_loading_tip.setBackgroundColor(color);
        }
    }

    public void setRealoadBgColor(@DrawableRes int res) {
        if (ll_show_load_data != null) {
            ll_show_load_data.setVisibility(VISIBLE);
            ll_show_load_data.setBackgroundResource(res);
        }
    }

    public void setRealoadVisibility(boolean showState) {
        if (ll_show_load_data != null) {
            ll_show_load_data.setVisibility(showState ? VISIBLE : GONE);
            if (showState) {
                ll_show_load_data.setBackgroundResource(R.drawable.shap_load);
            }
        }
    }

    public void setReloadTx(String reaload) {
        if (bt_operate != null) {
            bt_operate.setText(reaload);
        }
    }

    public void setReloadImag(@DrawableRes int resId) {
        if (img_show_load != null) {
            img_show_load.setVisibility(VISIBLE);
            img_show_load.setImageResource(resId);
        }
    }

    /**
     * 根据状态显示不同的提示
     *
     * @param loadStatus
     */
    public void setLoadingTip(LoadStatus loadStatus) {
        switch (loadStatus) {
            case empty:
                setVisibility(View.VISIBLE);
                setRealoadVisibility(false);
                img_tip_logo.setVisibility(View.VISIBLE);
                animationDrawable1.stop();
                progress.setVisibility(View.GONE);
                tv_tips.setText(getContext().getText(R.string.empty).toString());
                img_tip_logo.setImageResource(R.drawable.icon_nodata);
                break;
            case sereverError:
                setVisibility(View.VISIBLE);
                img_tip_logo.setVisibility(View.VISIBLE);
                animationDrawable1.stop();
                progress.setVisibility(View.GONE);
                if (TextUtils.isEmpty(errorMsg)) {
                    tv_tips.setText(getContext().getText(R.string.net_error).toString());
                } else {
                    tv_tips.setText(errorMsg);
                }
                img_tip_logo.setImageResource(R.drawable.ic_wrong);
                setRealoadVisibility(true);
                setReloadTx("点击刷新");
                break;
            case error:
                setVisibility(View.VISIBLE);
                img_tip_logo.setVisibility(View.VISIBLE);
                animationDrawable1.stop();
                progress.setVisibility(View.GONE);
                if (TextUtils.isEmpty(errorMsg)) {
                    tv_tips.setText(getContext().getText(R.string.no_net).toString());
                } else {
                    tv_tips.setText(errorMsg);
                }
                img_tip_logo.setImageResource(R.drawable.icon_nonet);
                setRealoadVisibility(true);
                setReloadTx("点击刷新");
                break;
            case loading:
                setVisibility(View.VISIBLE);
                setRealoadVisibility(false);
                img_tip_logo.setVisibility(View.GONE);
                animationDrawable1.start();
                progress.setVisibility(View.VISIBLE);
                tv_tips.setText(getContext().getText(R.string.loading).toString());
                break;
            case finish:
                setVisibility(View.GONE);
                break;
        }
    }

    public void setOnReloadListener(onReloadListener onReloadListener) {
        this.onReloadListener = onReloadListener;
    }

    /**
     * 重新尝试接口
     */
    public interface onReloadListener {
        void reload(View view);
    }
}